package com.ultrapower.hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;

/**
 * 
 * @author wangy
 *
 */
public class HbaseConnectionFactory {
	private final Connection connection;

	public HbaseConnectionFactory(Configuration config) {
		try {
			this.connection = ConnectionFactory.createConnection(config);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public Connection getConnection() {
		return connection;
	}

	public Table getTable(TableName tableName) {
		try {
			return connection.getTable(tableName);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public void releaseTable(Table table) {
		if (table == null) {
			return;
		}
		try {
			table.close();
		} catch (IOException ex) {
			throw new RuntimeException(ex);
		}
	}

	public void destroy() throws Exception {
		if (connection != null) {
			try {
				this.connection.close();
			} catch (IOException ex) {
				throw new RuntimeException(ex);
			}
		}
	}

}
